# -*- coding: utf-8 -*-
"""
Created on Fri Aug 17 10:45:50 2018

@author: Who

雷电财经
http://www.thunder.cc/

"""

import requests
import re
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.6 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_Kxun(url):
    text = get_html_text(url)
    if not text:
        return None
    regex = re.compile(r'<span class="date" id="[\s\S]*?">([\s\S]*?)</span>')
    dates = re.findall(regex, text)
    return dates


def get_dates_Xwen(url):
    text = get_html_text(url)
    if not text:
        return None
    regex = re.compile(r"<p>点击：[\s\S]*?次  / 栏目：<a href='/news/'>新闻</a> /时间：([\s\S]*?)</p>")
    dates = re.findall(regex, text)
    return dates


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        url = 'http://thunder.cc/fast/list_1_{}.html'
        url = url.format(a)
        dates = get_dates_Kxun(url)
        print('----雷电财经快讯第{}页获取中----'.format(a))
        if not dates:
            break
        for date in dates:
            s = day(date[:10])
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_two():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        url = 'http://thunder.cc/news/list_2_{}.html'
        url = url.format(a)
        dates = get_dates_Xwen(url)
        print('----雷电财经新闻第{}页获取中----'.format(a))
        if not dates:
            break
        for date in dates:
            s = day(date[:10])
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '雷电财经'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
#
